﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAP.Middleware.Connector;
using WC.BL.BE;
using WC.DL.DALC.IF;

namespace WC.DL.DALC.SAP
{
    public class CtaCteDALC : ICtaCteDALC
    {
        #region Singleton
        public static CtaCteDALC Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (typeof(CtaCteDALC))
                    {
                        if (_instance == null)
                        {
                            _instance = new CtaCteDALC();
                        }
                    }
                }
                return _instance;
            }
        }

        private CtaCteDALC() { }
        private static volatile CtaCteDALC _instance = null;
        #endregion


        public List<CtaCteBE> read(string pCodCliente, string pOrgVtas, DateTime pFecha)
        {
            //BSIDTable t_bsid = new BSIDTable();
            IRfcFunction function = null;
            RfcDestination dest;

            List<CtaCteBE> lstPartidas = null;
            string fechaSAP = ParametroDALC.dateNetToSap(pFecha);
            try
            {
                lstPartidas = new List<CtaCteBE>();
                dest = DALC.getDestination();
                function = dest.Repository.CreateFunction("ZWC_FI_001");

                function.SetValue("I_BUKRS", pOrgVtas);
                function.SetValue("I_KUNNR", pCodCliente);
                function.SetValue("I_AUGDT", fechaSAP);
                function.Invoke(dest);
                IRfcTable T_BSID = function.GetTable("T_BSID");
                CtaCteBE objPart = null;
                for (int i = 0; i < T_BSID.RowCount; i++)
                {
                    //set the current row - the row used for Get*/Set* operations
                    objPart = new CtaCteBE();
                    T_BSID.CurrentIndex = i;
                    //listBox1.Items.Add(T_BSID.GetString("KUNNR") + " -- " + T_BSID.GetString("NAME1"));
                    objPart.NomCliente = T_BSID.GetString("KONTL");
                    objPart.OrgVtas = T_BSID.GetString("BUKRS");
                    objPart.Ejercicio = T_BSID.GetString("GJAHR");
                    objPart.CondPago = T_BSID.GetString("ZTERM");
                    objPart.NumFactura = T_BSID.GetString("VBELN");
                    objPart.DocComp = T_BSID.GetString("AUGBL");
                    try{ objPart.FecComp = Convert.ToDateTime(T_BSID.GetString("AUGDT")); }
                    catch { objPart.FecComp = new DateTime(); }

                    try { objPart.FechaDoc = Convert.ToDateTime(T_BSID.GetString("BLDAT")); }
                    catch { objPart.FecComp = new DateTime(); }
                    try { objPart.FecVenc = Convert.ToDateTime(T_BSID.GetString("ZFBDT")); }
                    catch { objPart.FecComp = new DateTime(); }
                    objPart.CodCliente = T_BSID.GetString("KUNNR");
                    objPart.NumDocumento = T_BSID.GetString("BELNR");
                    objPart.IndImp = T_BSID.GetString("MWSKZ");
                    objPart.CodReferencia = T_BSID.GetString("XBLNR");
                    objPart.ClaseDoc = T_BSID.GetString("BLART");
                    objPart.Vencido = T_BSID.GetString("UMSKS").Equals("X") ? true : false;
                    objPart.Moneda = T_BSID.GetString("WAERS");
                    objPart.Importe = T_BSID.GetDecimal("WRBTR");
                    objPart.MonedaLocal = "PEN";
                    objPart.ImporteMonLocal = T_BSID.GetDecimal("DMBE2");//Moneda local 2
                    objPart.Texto = T_BSID.GetString("SGTXT");
                    //Fecha doc. => BLDAT
                    //Fecha pago => ZFBDT
                    //Cuenta  => KUNNR
                    //N° doc. => BELNR
                    //I1 => MWSKZ => Indicador de impuestos
                    //Referencia => XBLNR
                    //Cla => BLART => Clase de documento
                    //Ve => UMSKS => Indicador de vencido o no
                    //Venc. neto => ZFBDT => fecha Vencimiento neto
                    //MD => WAERS => Moneda
                    //Importe en MD => WRBTR
                    //ML => PEN => Moneda
                    //Importe en ML => DMBTR
                    //Texto => SGTXT
                    lstPartidas.Add(objPart);
                }
            }
            catch (Exception ex)
            {
                lstPartidas = null;
            }
            return lstPartidas;
        }

    }
}
